package com.itbaima.edu.info.manager.dao;

import com.itbaima.edu.info.manager.domain.Teacher;

/**
 * Created by byakuya on 2020/8/5
 */
public class TeacherDao {
    //老师数组
    private static Teacher[] teachers = new Teacher[5];

    public boolean addTeacher(Teacher t) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher =teachers[i];
            if (teacher == null){
                index = i;
                break;
            }
        }
        if (index == -1) //装满了
            return false;
        else{
            teachers[index] = t;
            return true;
        }

    }

    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public void deleteTeacherById(String id) {
        //查询ID在数组中的位置
        int index = getIndex(id);
        //使用null替换
        teachers[index] = null;
    }
    public int getIndex(String id){  //根据ID找索引
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(String id, Teacher newTeacher) {
        int index = getIndex(id);
        teachers[index] = newTeacher;
    }
}
